using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;

namespace WeChat2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string ini = @".\set.ini";
            string syspath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            string datapath = Path.Combine(syspath, "WeChat2");
            string ini = Path.Combine(datapath, "WeChat2.ini");
            if (!Directory.Exists(datapath))
            {
                Directory.CreateDirectory(datapath);
            }
            string num = numericUpDown1.Value.ToString();
            int num1 = int.Parse(num);
            if (File.Exists(ini))
            {

                ini = File.ReadAllText(ini);
                //var lines = File.ReadAllLines(ini);
                //string initxt = lines.First();
                string str = ini;
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
                p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
                p.StartInfo.CreateNoWindow = true;//不显示程序窗口
                p.Start();//启动程序

                //向cmd窗口发送输入信息
                int i = 0;
                while(i<num1-1)
                {
                    p.StandardInput.WriteLine("start \"\" \"" + str + "\"");
                    i++;

                }
                   
                
                p.StandardInput.WriteLine("start \"\" \"" + str + "\"" + "&exit");

                p.StandardInput.AutoFlush = true;


                p.WaitForExit();//等待程序执行完退出进程
                p.Close();


            }
            else
            {
                var ofd = new OpenFileDialog();
                ofd.Title = "请选择微信安装位置";
                ofd.FileName = "WeChat.exe";
                string path = string.Empty;
                //筛选文件类型
                ofd.Filter = "应用程序|*.exe";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    path = ofd.FileName;
                    //MessageBox.Show(path);

                    File.WriteAllText(ini, path, Encoding.UTF8);
                }

            }
        }
    }
}
